package com.ruibang.glass.equipment.service.impl.device;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.jdbc.StringUtils;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.equipment.domain.req.IubricationManagementReq;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.dto.IubricetionStatisticsDTO;
import com.ruibang.glass.equipment.entity.device.lubricationManagement;
import com.ruibang.glass.equipment.mapper.device.IubricationManagementMapper;
import com.ruibang.glass.equipment.service.IubricationManagementService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 周期给油脂管理服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Slf4j
@Service
public class IubricationManagementServiceImpl extends ServiceImpl<IubricationManagementMapper, lubricationManagement> implements IubricationManagementService {

    @Autowired
    private RedisSingleService redisService;

    /**
     * 周期给油脂管理
     *
     * @param managementReq
     * @return
     */
    @Override
    public IPage<lubricationManagement> queryPage(IubricationManagementReq managementReq) {
        IPage<lubricationManagement> page = baseMapper.queryPage(new Page<>(managementReq.getPageNo(), managementReq.getPageSize()), managementReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            Map<Object, Object> productMap = redisService.hmget(RedisKey.PRODUCT_LINE_GROUP_TYPE);
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            for (lubricationManagement record : page.getRecords()) {
                Object name = productMap.get(record.getProductLineId());
                if (name != null) {
                    record.setProductLineName(name.toString());
                }
                Object userName = userMap.get(record.getResponsiblePersonId());
                if (userName != null) {
                    record.setResponsiblePersonName(userName.toString());
                }
            }
        }
        return page;
    }

    /**
     * 新增跟新
     *
     * @param accidentRegister
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody updateOrAddById(lubricationManagement accidentRegister) {
        if (StringUtils.isNullOrEmpty(accidentRegister.getId())) {
            baseMapper.insert(accidentRegister);
            return ResultBody.success();
        }
        baseMapper.updateById(accidentRegister);
        return ResultBody.success();
    }

    /**
     * 周期统计
     *
     * @param accidentRegister
     * @return
     */
    @Override
    public ResultBody getStatistics(lubricationManagement accidentRegister) {
        List<IubricetionStatisticsDTO> iubricetionStatisticsDTOAll = baseMapper.getIubricetionStatisticsDTOAll(accidentRegister);
        List<IubricetionStatisticsDTO> iubricetionStatisticsDTOFinishCount = baseMapper.getIubricetionStatisticsDTOFinishCount(accidentRegister);
        List<IubricetionStatisticsDTO> iubricetionStatisticsDTOTimeCount = baseMapper.getIubricetionStatisticsDTOTimeCount(accidentRegister);
        for (IubricetionStatisticsDTO iubricetionStatisticsDTO : iubricetionStatisticsDTOAll) {
            for (IubricetionStatisticsDTO iubricetionStatisticsDTO1 : iubricetionStatisticsDTOFinishCount) {
                if (iubricetionStatisticsDTO.getWorkshop().equals(iubricetionStatisticsDTO1.getWorkshop())) {
                    iubricetionStatisticsDTO.setFinishCount(iubricetionStatisticsDTO1.getFinishCount());
                }
            }
            for (IubricetionStatisticsDTO iubricetionStatisticsDTO1 : iubricetionStatisticsDTOTimeCount) {
                if (iubricetionStatisticsDTO.getWorkshop().equals(iubricetionStatisticsDTO1.getWorkshop())) {
                    iubricetionStatisticsDTO.setTimeCount(iubricetionStatisticsDTO1.getTimeCount());
                }
            }
        }
        return ResultBody.success(iubricetionStatisticsDTOAll);
    }


}
